<template>
  <div id="Loc" v-loading="loading">
    <v-tableTitle
        :btnConfig="btnConfig"
        :tableSelectData="tableSelectData"
        :title="$t('config.loc')"
        @del="del"
        @exportFile="exportFile"
        @upFile="file"
        @newCreate="newCreate"
        @batchCreate="batchCreate"
        @downloadExcelFile="downloadExcelFile"
        @handleDownTemplate="downloadExcelFile"
        @handleDownErrorFile="exceptionFileUrl"
    />
    <v-table :page="page" :pageSize="pageSize" :tableConfig="tableConfig" :tableData="tableData" :total="total"
      @findData="getTableData" @getTableData="getTableData" @searhData="searhData" @selectData="selectData"
      @setdialog="setdialog" @upData="getTableData" ref="table"></v-table>
    <el-dialog :modal="false" :visible.sync="dialogFlag" title="" top="0" v-dialogDrag :close-on-click-modal="false"
      width="900px">
      <router-view></router-view>
    </el-dialog>
  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import config from "./Config";

export default {
  name: "loc",
  mixins: [baseMixin, config],
  data() {
    return {
      // 库位
      importType: 10325,
      loading: false,
      tableConfig: [
        {
          label: 'config.loc',
          prop: "loc",
          input: "input",
        },
        {
          label: 'common.type',
          prop: "locationtype",
          input: "select",
          options: this.getOptions('querycodeLkup/LOCTYPE')
        },
        {
          label: 'config.area',
          prop: "putawayzone",
          input: "select",
          options: this.getOptions('queryPutawayzoneDropDown')
        },
        {
          label: 'config.locFlag',
          prop: "locationflag",
          input: "select",
          options: this.getOptions('querycodeLkup/LOCFLAG')
        },
        {
          label: 'config.locCategory',
          prop: "locationcategory",
          input: "select",
          options: this.getOptions('querycodeLkup/LOCCATEGRY')
        },
        {
          label: 'config.loseId',
          prop: "loseid",
          input: "select",
          options: this.getOptions('querycodeLkup/YESNO')
        },
        {
          label: 'config.commingleLot',
          prop: "comminglelot",
          input: "select",
          options: this.getOptions('querycodeLkup/YESNO')
        },
        {
          label: 'config.commingleSku',
          prop: "comminglesku",
          input: "select",
          options: this.getOptions('querycodeLkup/YESNO')
        },
        {
          label: 'config.capacity',
          prop: "cubiccapacity",
          input: "input"
        }
      ],
      tableData: [],
      pageSize: 10,
      page: 1,
      total: 10,
      tableSelectData: [],
      tableSearhData: {},
      dialogFlag: false
    };
  },
  computed: {
    btnConfig() {
      return [
        { label: this.$t('config.batchCreate'), certigier: this.newStatus || !this.btnPermNameList.includes('WM030501'), invalid: "batchCreate" },
        { label: this.$t('common.create'), certigier: this.newStatus || !this.btnPermNameList.includes('WM030502'), invalid: "newCreate" },
        { label: this.$t('common.delete'), certigier: this.deleteStatus || !this.btnPermNameList.includes('WM030503'), disabled: true, invalid: "del" },
        // { type: "file1", certigier: this.poiStatus || !this.btnPermNameList.includes('WM030504'), label: this.$t('common.import'), invalid: "file" },
        {
          type: "file",
          // 权限编号 WM030504
          certigier: this.uploadStatus || !this.btnPermNameList.includes('WM030504'),
          label: this.$t('common.import'),
          invalid: "file",
          importType: '10325',
          importUrl: "importLoc",
          downTemplate: "handleDownTemplate",
          sheetKey: "poDetailList"
        },
        { label: this.$t('common.export'), certigier: this.poiStatus || !this.btnPermNameList.includes('WM030505'), disabled: true, invalid: "exportFile" },
      ]
    }
  },
  created() {
  },
  methods: {
    exceptionFileUrl(exceptionUrl) {

      const data = [{
        exceptionFileUrl: exceptionUrl
      }];

      // 异常下载配置
      let urlConf = {responseType: 'blob'};
      let exceptionFileName = "库位导入模板异常文件.xlsx";
      let url = '/comExcelImportResult/downloadExceptionFile';
      let options = {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'};

      try {
        this.$api.post(url, data, urlConf).then(res => {
          let blobParts = [res.data];
          if (window.navigator.msSaveBlob) {
            const blobObject = new Blob(blobParts);
            window.navigator.msSaveBlob(blobObject, exceptionFileName);
          } else {
            let tagName = 'a';
            const blob = new Blob(blobParts, options);
            const downloadElement = document.createElement(tagName);
            // 创建下载的链接
            const href = window.URL.createObjectURL(blob);
            downloadElement.style.display = 'none';
            downloadElement.href = href;
            // 下载后文件名
            downloadElement.download = exceptionFileName;
            document.body.appendChild(downloadElement);
            // 点击下载
            downloadElement.click();
            // 下载完成移除元素
            document.body.removeChild(downloadElement);
            // 释放掉blob对象
            window.URL.revokeObjectURL(href);
          }
          this.importVisualDialogFlag = false;
          this.initialize();
        });
      } catch (e) {
        this.$message.error(e);
      } finally {
        
      }
    },
    async downloadExcelFile(data) {
      try {
        let xlsName = "库位导入模板.xls";
        let url = "/downloadExcelFile/" + xlsName;
        let res = await this.$api.post(url, {}, {responseType: 'blob'})
        if (window.navigator.msSaveBlob) {
          const blobObject = new Blob([res.data]);
          window.navigator.msSaveBlob(blobObject, xlsName);
        } else {
          const blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'});
          const downloadElement = document.createElement('a');
          const href = window.URL.createObjectURL(blob); // 创建下载的链接
          downloadElement.style.display = 'none';
          downloadElement.href = href;
          downloadElement.download = xlsName; // 下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        }
      } catch (e) {
        this.$message.error(e);
      } finally {
        if (data.length !== 0) {
          
        }
      }
    },
    batchCreate() {
      this.$router.push({ name: "batchCreateLoc" })
    },
    async exportFile(data) {
      try {
        let res = await this.$api.post('/loc/exportloc', data, { responseType: 'blob' })
        if (window.navigator.msSaveBlob) {
          var blobObject = new Blob([res.data]);
          window.navigator.msSaveBlob(blobObject, this.$t('config.loc') + '.xls');
        } else {
          var blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8' });
          var filename = this.$t('config.loc') + '.xls';
          var downloadElement = document.createElement('a');
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.style.display = 'none';
          downloadElement.href = href;
          downloadElement.download = filename; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        }
      } catch (e) {
        this.$message.error(e);
      } finally {
        
      }
    },
    async file(data) {
      try {
        let res = await this.$api.post('/loc/importloc', data);
          this.$message.success(res.data.code);
          let page = sessionStorage.getItem('page');
          this.getTableData(page, this.tableSearhData);
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    async newCreate() {
      this.$store.commit("setNewFlag", true);
      this.dialogFlag = true;
      setTimeout(() => {
        this.$router.push({ name: 'locDeawer' });
      }, 0)
    },
    async del(data) {
      try {
        let res = await this.$api.post(`/deleteLoc`, data);
        
          this.$message.success(res.returnMgr);
          let page = sessionStorage.getItem('page');
          this.getTableData(page, this.tableSearhData);
          
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    //选中的内容
    selectData(data) {
      this.tableSelectData = data;
    },
    async getTableData(page, data) {
      try {
        this.loading = true;
        let res = await this.$api.post(`/queryLoc/${page}`, data);
        
          this.tableData = res.page.list;
          this.page = res.page.curpage;
          this.pageSize = res.page.pageSize;
          this.total = res.page.total;
      } catch (e) {
        this.$message.error(e);
      } finally {
        this.loading = false;
      }
    },
    //搜索的值
    searhData(data, up) {
      if (!up) {
        this.tableSearhData = data;
      } else {
        this.tableSearhData = data;
        this.getTableData(1, this.tableSearhData);
      }
    },
    //设置弹框
    setdialog(data) {
      this.$store.commit("setRouteData", data);
      this.dialogFlag = true;
      setTimeout(() => {
        this.$router.push({ name: 'locDeawer' });
      }, 0)
    },
  },
  mounted() {
    
    this.$store.commit("setSkip", {});
    this.$store.commit("setNewFlag", false);
    this.$store.commit("setRouteData", "");
    this.getTableData(1, {});
  },
  watch: {
    tableSelectData(data) {
      if (this.permissionStatus) {
        if (data.length > 0) {
          this.deleteStatus = false;
        } else {
          this.deleteStatus = true;
        }
      }
    },
    $route(data) {
      if (data.name === "loc") {
        this.dialogFlag = false;
      }
    },
    dialogFlag(data) {
      if (data === false) {
        this.$store.commit("setNewFlag", false);
        this.getTableData(1, {});
        this.$router.push({ name: "loc" });
        this.$store.commit("setSkip", {});
        this.$store.commit("setRouteData", {});
      }
    }
  },
  components: {
    "v-tableTitle": tableTitle,
    "v-table": table,
  }
};
</script>

<style scoped>
#Loc {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
